package quickConnect;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

import org.quickconnect.ControlObject;

import userInterface.ChatSessionPanel;
import userInterface.MainFrame;

import beans.MessageBean;
import beans.SessionBean;

public class ReceiveMessageBCO implements ControlObject {

	@Override
	public Object handleIt(ArrayList<Object> arg0) {

		HashMap params = (HashMap)arg0.get(0);
		MessageBean msg = (MessageBean) params.get("message");
		
		ArrayList<ChatSessionPanel> sessions = MainFrame.mainFrame.getChatSessions();
		
		for(int i = 0; i < sessions.size(); i++){
			SessionBean curSession = sessions.get(i).getSession();
			if(curSession.getSessionId().equals(msg.getSessionid())){
				String timestamp = msg.getTimestamp().getHours() + ":" + msg.getTimestamp().getMinutes();
				try
				{
				    Document doc = sessions.get(i).getaMessengerPanel().getReceiveArea().getDocument();
				    if(params.containsKey("private"))
				    {
				    	doc.insertString(doc.getLength(),"[" + timestamp + "] " + msg.getSender()+ " > " +msg.getMessage() + "\n", sessions.get(i).getaMessengerPanel().privateMsg);
				    }
				    else
				    {
				    	doc.insertString(doc.getLength(),"[" + timestamp + "] " + msg.getSender()+ " > " +msg.getMessage() + "\n", sessions.get(i).getaMessengerPanel().publicMsg);				    	
				    }
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				break;
			}
		}
		
		return null;
	}

}
